﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ShoppingEntity.DistributionInfo;

namespace ShoppingBll
{
    public class DistributionInfoBll : BaseBll<DistributionInfo>, IDistributionInfoBll
    {
		private IDistributionInfoDal _distributionInfoDal;
		private IOrderInfoDal _orderInfoDal;
		private IUserInfoDal _userInfoDal;
		private ICommodityInfoDal _commodityInfoDal;
		private ShoppingDbContext _dbContext;
		
		public DistributionInfoBll(ShoppingDbContext dbContext,IDistributionInfoDal distributionInfoDal, IOrderInfoDal orderInfoDal, IUserInfoDal userInfoDal, ICommodityInfoDal commodityInfoDal) : base(distributionInfoDal)
		{
			_distributionInfoDal = distributionInfoDal;
			_orderInfoDal = orderInfoDal;
			_userInfoDal = userInfoDal;
			_commodityInfoDal = commodityInfoDal;
			_dbContext= dbContext;
		}
		/// <summary>
		/// (分页)显示列表数据
		/// </summary>
		/// <param name="page"></param>
		/// <param name="limit"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public object GetDistributionInfoByPage(int page, int limit, out int count)
		{
			//获取数据库中配送全部的数据（未真实查询）
			var distributionInfos = _distributionInfoDal.GetEntitiesDb().AsQueryable();

			//连表
			var tempDist = from d in distributionInfos

						   join c in _commodityInfoDal.GetEntitiesDb()

						   on d.GoodsId equals c.ID into dcTemp
						   from dc in dcTemp.DefaultIfEmpty()

						   join u in _userInfoDal.GetEntitiesDb().Where(u => !u.IsDelete)
						   on d.UserId equals u.ID into duTemp
						   from du in duTemp.DefaultIfEmpty()

						   join u2 in _userInfoDal.GetEntitiesDb().Where(u => !u.IsDelete)
						   on d.UserId equals u2.ID into du2Temp
						   from du2 in du2Temp.DefaultIfEmpty()
						   select new
						   {
							   d.ID,
							   d.OrderID,
							   dc.CommodityName,
							   du.UserName,
							   du.Address,
							   DistributionName = du2.UserName,
							   d.State
						   };

			//查询出来数据的数量
			count = tempDist.Count();
			//分页
			tempDist = tempDist.Skip((page - 1) * limit).Take(limit);

			var list = tempDist.ToList().Select(d => 
			{
				string State = d.State.ToString();
				return new
				{
					d.ID,
					d.OrderID,
					d.CommodityName,
					d.UserName,
					d.Address,
					d.DistributionName,
					State 
				};

			});

			return list;
		}
		/// <summary>
		/// 获取配送状态下拉
		/// </summary>
		/// <returns></returns>
		public object GetStatusSelectOption()
		{
			List<object> options = new List<object>();

			//获取枚举中所有的名字
			var names = Enum.GetNames(typeof(DistributionInfoStateEnum));

			foreach (var namesItem in names)
			{
				var value = (int)Enum.Parse(typeof(DistributionInfoStateEnum),namesItem);
				options.Add(new
				{
					Key = namesItem,
					Value = value
				});
			}
			return options;
		}


		/// <summary>
		/// 商品已送达顾客手里
		/// </summary>
		/// <param name="distributionInfoId"></param>
		/// <returns></returns>
		public bool GoodDelivered(string distributionInfoId)
        {
			DistributionInfo distributionInfo = _distributionInfoDal.GetEntity(distributionInfoId);
            if (distributionInfo == null)
            {
				return false;
            }

			distributionInfo.State = DistributionInfo.DistributionInfoStateEnum.已送达;

			using (var transaction = _dbContext.Database.BeginTransaction())
			{
				bool isSuccess1 = _distributionInfoDal.Update(distributionInfo);

				OrderInfo orderInfo = _orderInfoDal.GetEntity(distributionInfo.OrderID);
				orderInfo.State = OrderInfo.OrderStateEnum.已送达;
				bool isSuccess2 = _orderInfoDal.Update(orderInfo);

                if (isSuccess1 && isSuccess2)
                {
					transaction.Commit();
					return true;
                }
                else
                {
					transaction.Rollback();
					return false;
                }

			}

        }
    }
}
